package com.base.system.managedBean;

import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.annotation.SelectItems;
import org.operamasks.faces.component.layout.impl.UIPanel;
import org.operamasks.faces.user.util.Browser;

import com.base.commonKey.entity.CommonKey;
import com.base.commonKey.entity.CommonKeyType;
import com.base.system.managedBean.baseBean.BaseCommonKeyBean;

@ManagedBean(name = "system_editCommonKeyItemBean", scope = ManagedBeanScope.REQUEST)
public class EditCommonKeyItemBean extends BaseCommonKeyBean {
	private final static Log logger = LogFactory
			.getLog(ShowCommonKeyBean.class);
	@Inject
	FacesContext context;

	@Accessible
	private boolean parentRendered = true;

	@SaveState
	@Accessible
	private String keyType;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			Map<String, Object> session = context.getExternalContext()
					.getSessionMap();
			id = context.getExternalContext().getRequestParameterMap().get(
					"commonKeyId");
			commonKey = commonKeyService.getCommonKeyById(Integer.parseInt(id));
			keyType = commonKey.getKeyType();
			CommonKeyType ckt = commonKeyService
					.getCommonKeyTypeByTypeCode(keyType);
			if (ckt.getIsLay() == true) {
				parentRendered = true;
			} else {
				parentRendered = false;
			}
			// System.out.println("parentRendered:" + parentRendered);
			if (commonKey.isCheckEnable()) {
				styleNo = "display:block";
				styleYes = "display:none";
			} else {
				styleNo = "display:none";
				styleYes = "display:block";
			}
			this.getCommonKeyProperty();

			boolean eb = commonKey.isEnabled();
			if (eb == true) {
				enabled1 = "是";
			} else if (eb == false) {
				enabled1 = "否";
			}
			boolean ce = commonKey.isCheckEnable();
			if (ce == true) {
				checkEnable1 = "是";
			} else if (ce == false) {
				checkEnable1 = "否";
			}
			parentTypeId = parentId;
			CommonKey ck = commonKey.getCommonKey();
			if (ck != null) {
				parentTypeName = ck.getKeyValue();
				parentId = ck.getId().toString();
			}
		}
	}

	@Bind(id = "styleYes", attribute = "value")
	public String styleYes = "display:none";

	@Bind(id = "styleNo", attribute = "value")
	public String styleNo = "display:none";

	@Bind(id = "mainShowCK")
	private UIPanel mainShowCK;
	@Accessible
	private SelectItem[] enabled1_items = { new SelectItem("是", "是"),
			new SelectItem("否", "否") };
	@SaveState
	@Bind(id = "enabled1")
	@SelectItems(source = "#{this.enabled1_items}")
	private String enabled1;
	@Accessible
	private SelectItem[] checkEnable1_items = { new SelectItem("是", "是"),
			new SelectItem("否", "否") };
	@SaveState
	@Bind(id = "checkEnable1")
	@SelectItems(source = "#{this.checkEnable1_items}")
	private String checkEnable1;

	@Bind
	@SaveState
	private String parentTypeId;
	@Bind
	@SaveState
	private String parentTypeName;

	@Action(id = "save")
	public void save() {
		CommonKey commonKey = commonKeyService.getCommonKeyById(Integer
				.parseInt(id));
		if (parentTypeId != null && !"".equals(parentTypeId)) {
			CommonKey ck = commonKeyService.getCommonKeyById(Integer
					.valueOf(parentTypeId));
			commonKey.setCommonKey(ck);
		}

		if (checkEnable1.equals("是")) {
			commonKey.setCheckEnable(true);
		} else {
			commonKey.setCheckEnable(false);
		}
		if (enabled1.equals("是")) {
			commonKey.setEnabled(true);
		} else {
			commonKey.setEnabled(false);
		}
		commonKey.setKeyValue(keyValue);
		commonKey.setIndexNumber(indexNumber);
		commonKeyService.updateCommonKey(commonKey);
		Browser
				.execClientScript("window.parent.gridReloadJsvar.fireEvent('click');");
	}

	@Action(id = "back")
	public void back() {
		Browser
				.execClientScript("window.parent.edtiCloseJsvar.fireEvent('click');");
	}
}
